package leetcode;

import java.util.*;

/**
 * 描述:49. 字母异位词分组
 *
 * @author Madison You
 * @created 16:26
 */
public class Mid_49_GroupAnagrams {

    public static void main(String[] args) {
        String[] str = {""};
        List<List<String>> lists = new Mid_49_GroupAnagrams().groupAnagrams(str);
        for (List<String> list : lists) {
            System.out.println(Arrays.toString(list.toArray()));
        }
    }


    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> list = new ArrayList<>();
        Map<String, List<String>> map = new HashMap<>();
        for (int i = 0; i < strs.length; i++) {
            String str = sort(strs[i], new StringBuilder());
            List<String> strings = map.get(str);
            if (Objects.isNull(strings)) {
                List<String> strings1 = new ArrayList<>();
                strings1.add(strs[i]);
                map.put(str, strings1);
            } else {
                strings.add(strs[i]);
                map.put(str, strings);
            }
        }
        for (Map.Entry<String, List<String>> stringListEntry : map.entrySet()) {
            list.add(stringListEntry.getValue());
        }
        return list;
    }

    public String sort(String str,StringBuilder sb) {
        char[] chars = str.toCharArray();
        Arrays.sort(chars);
        for (char aChar : chars) {
            sb.append(aChar);
        }
        return sb.toString();
    }

}
